package com.asj.house.service.impl;

import com.asj.house.entity.HouseIntermediary;
import com.asj.house.entity.HouseRoom;
import com.asj.house.entity.HouseSubscribe;
import com.asj.house.entity.HouseUser;
import com.asj.house.mapper.HouseSubscribeMapper;
import com.asj.house.returns.RespCode;
import com.asj.house.returns.RespTable;
import com.asj.house.service.HouseIntermediaryService;
import com.asj.house.service.HouseRoomService;
import com.asj.house.service.HouseSubscribeService;
import com.asj.house.service.HouseUserService;
import com.asj.house.vo.param.UserParam;
import com.asj.house.vo.result.SubscribeResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 预约表 服务实现类
 * </p>
 *
 * @author asj
 * @since 2021-03-16
 */
@Service
public class HouseSubscribeServiceImpl extends ServiceImpl<HouseSubscribeMapper, HouseSubscribe> implements HouseSubscribeService {

    @Autowired
    private HouseUserService houseUserService;

    @Autowired
    private HouseRoomService houseRoomService;

    @Autowired
    private HouseIntermediaryService houseIntermediaryService;

    @Override
    public RespTable<List<SubscribeResult>> listSubscribe(UserParam userParam) {

        QueryWrapper<HouseSubscribe> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(HouseSubscribe::getDeleted, 0)
                .orderByDesc(HouseSubscribe::getId);

        IPage<HouseSubscribe> iPage = new Page<>(userParam.getPage(), userParam.getLimit());
        IPage<HouseSubscribe> page = super.page(iPage, queryWrapper);
        List<HouseSubscribe> subscribeList = page.getRecords();

        //数据组装
        List<SubscribeResult> subscribeResultList = dataPackage(subscribeList);

        return new RespTable(RespCode.OK, subscribeResultList, page.getTotal());
    }

    private List<SubscribeResult> dataPackage(List<HouseSubscribe> subscribeList) {
        List<SubscribeResult> subscribeResultList = Lists.newArrayList();

        for (HouseSubscribe houseSubscribe : subscribeList) {
            HouseUser houseUser = houseUserService.getById(houseSubscribe.getUserId());
            HouseRoom houseRoom = houseRoomService.getById(houseSubscribe.getRoomId());
            HouseIntermediary houseIntermediary = houseIntermediaryService.getById(houseSubscribe.getIntermediaryId());

            SubscribeResult subscribeResult = SubscribeResult.builder()
                    .id(houseSubscribe.getId())
                    .name(houseUser.getName())
                    .mobile(houseUser.getMobile())
                    .num(houseSubscribe.getNum())
                    .date(houseSubscribe.getTime())
                    .houseName(houseRoom.getName())
                    .houseUrl(houseRoom.getUrl())
                    .zjName(houseIntermediary.getName())
                    .zjMobile(houseIntermediary.getMobile())
                    .zjUrl(houseIntermediary.getUrl())
                    .type(houseSubscribe.getType())
                    .build();

            subscribeResultList.add(subscribeResult);
        }

        return subscribeResultList;
    }
}
